﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class TimeLineControl : MonoBehaviour
{
    public Dictionary<string, PlayableDirector> playableDirectors = new Dictionary<string, PlayableDirector>();
    private Dictionary<string, PlayableDirector> dicLoopTimeLine = new Dictionary<string, PlayableDirector>();
    public List<PlayableDirector> TimeLineGroups = new List<PlayableDirector>();

    public static TimeLineControl Instance;
    private void Awake()
    {
        Instance = this;
    }

    // Use this for initialization
    void Start()
    {
        FindAllTimeInChild();
        Debug.Log(playableDirectors.Count);
    }

    /// <summary>
    /// 找到所有子物体中的TimeLine
    /// </summary>
    private void FindAllTimeInChild()
    {
        List<GameObject> tempObj = ToolUnity.GetAllChildGameObject(transform);
        foreach (var item in tempObj)
        {
            if (item.GetComponent<PlayableDirector>() != null)
            {
                playableDirectors.Add(item.name, item.GetComponent<PlayableDirector>());
            }
        }
    }

    /// <summary>
    /// 播放TimeLine
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public PlayableDirector PlayTimeLine(string name, DirectorWrapMode directorWrapMode = DirectorWrapMode.None)
    {
        if (playableDirectors.ContainsKey(name))
        {
            if (playableDirectors[name].state != PlayState.Playing)
            {
                playableDirectors[name].Play();
                playableDirectors[name].extrapolationMode = directorWrapMode;
            }
            return playableDirectors[name];
        }
        else
        {
            Debug.LogError("没有名叫：" + name);
            return null;
        }
    }

    /// <summary>
    /// 一组动画每次只播放一个
    /// </summary>
    /// <param name="GroupName"></param>
    /// <param name="isloop"></param>
    public void PlayTimeLieGroup(string GroupName, string name, bool isloop = false)
    {
        for (int i = 0; i < TimeLineGroups.Count; i++)
        {
            if (TimeLineGroups[i].name == GroupName)
            {
                GameObject temp = ToolUnity.FindGameObject(TimeLineGroups[i].gameObject, name);
                if (temp && temp.GetComponent<PlayableDirector>())
                {
                    PlayableDirector playableDirector = temp.GetComponent<PlayableDirector>();
                    if (playableDirector.state == PlayState.Playing)
                    {
                        Debug.LogWarning(name + "正在播放");
                    }
                    else
                    {
                        playableDirector.Play();
                    }
                }
                return;
            }
        }
        Debug.LogError("没有找到组名为：" + GroupName);
    }

    /// <summary>
    /// 停止播放Timeline
    /// </summary>
    public void StopTimeLine(string name)
    {
        if (playableDirectors.ContainsKey(name))
        {
            if (playableDirectors[name].state == PlayState.Playing)
            {
                playableDirectors[name].Stop();
            }
            else
            {
                Debug.LogWarning(name + "已经停止无法让他继续停止");
            }
        }
        else
        {
            Debug.LogError("没有名叫：" + name);
        }
    }

    /// <summary>
    /// 结束当前所有TimeLine 播放
    /// </summary>
    public void StopAllTimeLine()
    {
        foreach (var item in playableDirectors)
        {
            if (item.Value.state == PlayState.Playing)
            {
                item.Value.Stop();
            }
        }
    }

    private void Update()
    {
        foreach (var item in dicLoopTimeLine)
        {
            if (item.Value.state != PlayState.Playing)
            {
                item.Value.Play();
            }
        }
    }
}
